package com.gdk.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.gdk.auth.application.convert.AuthRoleDTOConverter;
import com.gdk.auth.application.convert.AuthUserDTOConverter;
import com.gdk.auth.application.dto.AuthRoleDTO;
import com.gdk.auth.application.dto.AuthUserDTO;
import com.gdk.auth.common.entity.Result;
import com.gdk.auth.domain.entity.AuthRoleBO;
import com.gdk.auth.domain.entity.AuthUserBO;
import com.gdk.auth.domain.service.AuthRoleDomainService;
import com.gdk.auth.domain.service.AuthUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色controller
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名称不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色key不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE
                    .convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        }catch (Exception e) {
            log.error("RoleController.add.dto:{}", e.getMessage(),e);
            return Result.fail("新增角色失败");
        }
    }

    /**
     * 更新角色信息
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("RoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE
                    .convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        }catch (Exception e) {
            log.error("RoleController.update.dto:{}", e.getMessage(),e);
            return Result.fail("更新角色信息失败");
        }
    }

    /**
     * 删除角色
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE
                    .convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.delete(authRoleBO));
        }catch (Exception e) {
            log.error("RoleController.delete.dto:{}", e.getMessage(),e);
            return Result.fail("删除角色失败");
        }
    }

}
